<?php

namespace iBrand\Member\Backend\Console;

use Illuminate\Console\Command;
use DB;

class MemberBackendMenusCommand extends Command
{
	protected $signature = 'import:member-backend-menus';

	protected $description = 'import member backend menus';

	public function handle()
	{
		$lastOrder     = DB::table(config('admin.database.menu_table'))->max('order');
		$memberTopMenu = DB::table(config('admin.database.menu_table'))->where('title', '会员管理')->where('parent_id', 0)->first();
		if (!$memberTopMenu) {
			$memberTopMenuId = DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => 0,
				'order'      => $lastOrder++,
				'title'      => '会员管理',
				'icon'       => 'iconfont icon-huiyuanguanli-',
				'blank'      => 1,
				'uri'        => 'member/users',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		} else {
			$memberTopMenuId = $memberTopMenu->id;
		}

		$member = DB::table(config('admin.database.menu_table'))->where('title', '会员管理')->where('parent_id', $memberTopMenuId)->first();
		if (!$member) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $memberTopMenuId,
				'order'      => $lastOrder++,
				'title'      => '会员管理',
				'icon'       => 'iconfont icon-huiyuanguanli--',
				'blank'      => 1,
				'uri'        => 'member/users',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$pointRecord = DB::table(config('admin.database.menu_table'))->where('title', '会员积分记录')->where('parent_id', $memberTopMenuId)->first();
		if (!$pointRecord) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $memberTopMenuId,
				'order'      => $lastOrder++,
				'title'      => '会员积分记录',
				'icon'       => 'iconfont icon-huiyuanjifenjilu',
				'blank'      => 1,
				'uri'        => 'member/points',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$balanceRecord = DB::table(config('admin.database.menu_table'))->where('title', '会员余额记录')->where('parent_id', $memberTopMenuId)->first();
		if (!$balanceRecord) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $memberTopMenuId,
				'order'      => $lastOrder++,
				'title'      => '会员余额记录',
				'icon'       => 'iconfont icon-huiyuanjifenjilu',
				'blank'      => 1,
				'uri'        => 'member/balances',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}

		$recharge = DB::table(config('admin.database.menu_table'))->where('title', '储值管理')->where('parent_id', $memberTopMenuId)->first();
		if (!$recharge) {
			DB::table(config('admin.database.menu_table'))->insertGetId([
				'parent_id'  => $memberTopMenuId,
				'order'      => $lastOrder++,
				'title'      => '储值管理',
				'icon'       => 'iconfont icon-zhifushezhi',
				'blank'      => 1,
				'uri'        => 'member/recharge',
				'created_at' => date('Y-m-d H:i:s', time()),
				'updated_at' => date('Y-m-d H:i:s', time()),
			]);
		}
	}
}